package com.happypets.backstage;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.happypets.listener.Test;
import com.happypets.servicesImpl.PetServiceImpl;

public class StatsDeal extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public StatsDeal() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		response.setHeader("Cache-Control", "no-store"); 
		
		PetServiceImpl psi = new PetServiceImpl();
		
//		HttpSession session = request.getSession();
		
		PrintWriter out = response.getWriter();
		
		List<Map<String,String>> lm = null;
		
		String flag = request.getParameter("flag");
		
		String start = request.getParameter("start");
		String end = request.getParameter("end");
		
		//1 hour  2 day 3 month
		
		
		
		switch(flag){
		case "1" : lm = psi.statsByHour(start, end);	break;
		case "2" : lm = psi.statsByDay(start, end); 	break;
		case "3" : lm = psi.statsByMonth(start, end);	break;
		case "4" : lm = psi.statsWeekend(7);				break;
		case "5" : lm = psi.recentForm();					break;
		case "6" : 
			lm = psi.otherSet();	
			Map<String,String> ad = lm.get(0);
			int olNum = Test.olNum;
			
			ad.put("OLNUM", olNum+"");
			
		break;
		}
		
		
		
		Gson gson=new Gson();
		String json=gson.toJson(lm);
		System.out.println(json);
		out.write(json);
		
		out.flush();
		out.close();
		
		
		
		
		
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
